package com.aidansmeaton.weightmanager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.aidansmeaton.weightmanager.data.DatabaseHelper;
import com.aidansmeaton.weightmanager.util.CalorieHelper;

/** The first activity in the app. */
public class SetUpGoalsActivity extends Activity {
	
	private static int DEFAULT_EXERCISE_MINUTES = 60;

	private DatabaseHelper db;
	private TextView weightText;
	private TextView dateText;
	private TextView caloriesText;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_goals);

		setTitle("Setup - Goals");

		db = DatabaseHelper.getInstance(this);
		
		weightText = (TextView) findViewById(R.id.target_weight);
		caloriesText = (TextView) findViewById(R.id.planEnergy);

		//Calculate daily goal
		Cursor c = db.getWeightLogs();
		c.moveToFirst();
		int age = getSharedPreferences("Profile",0).getInt("Age",-1);
		int currentWeight = c.getInt(DatabaseHelper.WEIGHT_LOG_WEIGHT);	// get most recent weight log
		boolean isFemale = getSharedPreferences("Profile", 0).getBoolean("Gender",true);
		int exercise_selection = getSharedPreferences("Profile",0).getInt("Exercise", 0);
		boolean imperial = getSharedPreferences("Profile",0).getBoolean("TargetUnits", true);
		int plan = CalorieHelper.calculateRecommendedCaloriesPlan(age, currentWeight, isFemale, exercise_selection, imperial);
		int weight = getSharedPreferences("Profile",0).getInt("TargetWeight",-1);
		
		weightText.setText(""+CalorieHelper.weightToString(weight,imperial));
		caloriesText.setText(""+plan);

		// Stop keyboard popping up automatically
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

	}
	

	private boolean setUpGoals() {
		
		// get weights and dates from widgets
		boolean success = true;
		// input validation
		try {
			getSharedPreferences("Profile", 0).edit().putInt("ExerciseTarget", DEFAULT_EXERCISE_MINUTES).commit();
		// catch invalid input
		} catch (NumberFormatException e) {
			success = false;
		}
		// save if valid input
		if (success) {
		}
		return success;
	}

	public void finishClick(View v) {
		if (setUpGoals()) {
			getSharedPreferences("Profile", 0).edit().putBoolean("Complete", true).commit();
			finish();
		}
	}

	public void backClick(View v) {
		if (setUpGoals()) {
			finish();
			Intent setupIntent = new Intent(this, SetUpTargetWeightActivity.class);
			this.startActivity(setupIntent);
		}		
	}

	public void laterClick(View v) {
		finish();
	}

}